<?php

/**
 * @autor EaDeveloper 
 */
class ModReports implements EaSoftModule {

    public $lastQuery;
    public $includePath;
    public $instanceName = "ModReports";
    public $db;
    public static $meses = array("Enero", "Febrero", "Marzo", "Abril",
        "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");
    public $reports;
    public $currentRep;
    public $printers = array("excel" => array("name" => "EaExcelPrinter",
            "label" => "Excel",
            "icon" => "excel.png"),
        "pdf" => array("name" => "EaPdfPrinter",
            "label" => "Pdf", "icon" => "pdf.png"));

    public function getCssFiles() {
        ?>
        <link href="<?php echo RELATIVE_PATH; ?>js_v2/jquery-ui-1.8.7.custom/development-bundle/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
        <link href="<?php echo RELATIVE_PATH; ?>css/tablereportes.css" rel="stylesheet" type="text/css" />
        <?php
    }

    public function getJsFiles() {
        ?>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/jquery-ui-1.8.7.custom/js/jquery-ui-1.8.7.custom.min.js"></script>
        <script type="text/javascript"  src="<?php echo RELATIVE_PATH; ?>js_v2/maintenancev2.js"  > </script>
        <?php
    }

    public function getWindowTitle() {
        return "Reportes";
    }

    private function getListJson($query, $pkField, $descField) {
        $r = $this->db->queryToArray($query);
        $j = array();
        foreach ($r as $rws) {
            array_push($j, array("id" => $rws[$pkField], "value" => $rws[$pkField] . "-" . $rws[$descField]));
        }
        echo json_encode($j);
    }

    /**
     * En el futuro puede cargar un xml o desde la db
     */
    private function loadReports() {
        if ($this->reports == null) {
            $rep1 = new RepoDeudoresImpl();
            $rep2 = new RepoEcImpl();
            $rep3 = new RepoCobrosGeneralImpl();
            $rep4 = new RepoAuditCobrosImpl();
            $rep1->db = $this->db;
            $rep2->db = $this->db;
            $rep3->db = $this->db;
            $rep4->db = $this->db;
            $this->reports["deudores"] = $rep1;
            $this->reports["estadocuentas"] = $rep2;
            $this->reports["auditcobros"] = $rep3;
            $this->reports["cobros"] = $rep4;
        }
    }

    public function main() {
        $this->loadReports();
        $option = getParam("report");
        switch ($option) {
            case "listalumnos": {
                    $p = getParam("term");
                    $p = isEmpty($p) ? null : "where ID LIKE '$p%'";
                    $q = "select ID,CONCAT(FIRST_NAME,' ',LAST_NAME) AS NAME from cllg_student $p";
                    $this->getListJson($q, "ID", "NAME", $p);
                    break;
                }
            case "listbenef": {
                    $p = getParam("term");
                    $p = isEmpty($p) ? null : "where ID LIKE '$p%'";
                    $q = "select ID, RAZON_SOCIAL from cllg_benefactor $p";
                    $this->getListJson($q, "ID", "RAZON_SOCIAL", $p);
                    break;
                }
            case "print": {
                    $pri = getParam("printer");
                    $idrep = getParam("reportId");
                    $printer = new $pri();
                    if (isset($this->reports[$idrep])) {
                        $rp = $this->reports[$idrep];
                        $printer->db = $this->db;
                        $printer->write($rp);
                        break;
                    }
                    break;
                }
            default: {
                    if (isset($this->reports[$option])) {
                        $this->currentRep = $this->reports[$option];
                        $this->currentRep->key = $option;
                        $this->currentRep->db = $this->db;
                        $params = getParam("params");
                        if (isEmpty($params)) {
                            include MODULES . DS . "reportes/mainReporte.php";
                        } else {
                            include MODULES . DS . "reportes/resultReporte.php";
                        }
                    } else {
                        include MODULES . DS . "reportes/main.php";
                    }
                }
        }
    }

}
?>
